package com.efounder.news.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.efounder.mobilenews.R;
import com.efounder.news.bean.NewsListBean;
import com.efounder.utils.ResStringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * sunlp on 2017/10/10
 */

public class NewsListAdapter extends RecyclerView.Adapter {
    private final String TAG = NewsListAdapter.class.getSimpleName();

    private static final int ONLY_TEXT = 0;//纯文字
    private static final int SMALL_IMG = 1;//右边一张小图
    private static final int THREE_IMG = 2;//底部三张图
    private static final int BIG_IMG = 3;//底部一张大图

    private Context mContext;

    private List<NewsListBean> mNewsListBeanList;
    private OnItemClickListener mOnItemClickListener;


    public NewsListAdapter(Context context, List<NewsListBean> newsListBeanList){
        mContext = context;
        mNewsListBeanList = newsListBeanList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 根据viewType返回不同的view，此viewtype从getItemViewType方法中获得
        View view;
        if(viewType == SMALL_IMG){
            view = LayoutInflater.from(mContext).inflate(R.layout.item_pic_video_news, parent,false);
            return new SmallImgViewHolder(view);
        }else if(viewType == THREE_IMG){
            view = LayoutInflater.from(mContext).inflate(R.layout.item_three_pics_news, parent,false);
            return new ThreeImgViewHolder(view);
        }else if(viewType == BIG_IMG){
            view = LayoutInflater.from(mContext).inflate(R.layout.item_center_pic_news, parent,false);
            return  new BigImgViewHolder(view);
        }else{
            view = LayoutInflater.from(mContext).inflate(R.layout.item_text_news, parent,false);
            return new OnlyTextViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NewsListBean newsListBean = mNewsListBeanList.get(position);
        final BaseViewHolder baseViewHolder = (BaseViewHolder)holder;

        int resourceType = newsListBean.getResourceType();
        //存在多张图片
        List<NewsListBean.ImgBean> imgBeanList = newsListBean.getImgBeanList();
        String imgSrc = newsListBean.getImgsrc();
        String title = newsListBean.getTitle();
        String logo = newsListBean.getLogo();
        String logoColor = newsListBean.getLogoColor();
        String fromWhere = newsListBean.getFromWhere();
        String interactions = newsListBean.getInteractions();
        String newsTime = newsListBean.getNewsTime();

        String videoTotalDuration = newsListBean.getVideoTotalDuration();

        if(getItemViewType(position) == SMALL_IMG){
            //图在右边的情况
            SmallImgViewHolder smallImgViewHolder = (SmallImgViewHolder) holder;
            setNetPicture(imgBeanList.get(0).getImgsrc(), smallImgViewHolder.iv_img);
            //区分视频或是图片
//            if(resourceType == 0){
                smallImgViewHolder.ll_duration.setVisibility(View.GONE);
//            }else if(resourceType == 1){
//                smallImgViewHolder.tv_duration.setText(videoTotalDuration);
//            }

        }else if(getItemViewType(position) == THREE_IMG){
            //底部三张图片的情况
            ThreeImgViewHolder threeImgViewHolder = (ThreeImgViewHolder) holder;
            for(int j = 0; j < imgBeanList.size(); j++){
                if(j == 0){
                    setNetPicture(imgBeanList.get(j).getImgsrc(), threeImgViewHolder.iv_img1);
                }else if(j == 1){
                    setNetPicture(imgBeanList.get(j).getImgsrc(), threeImgViewHolder.iv_img2);
                }else if(j == 2){
                    setNetPicture(imgBeanList.get(j).getImgsrc(), threeImgViewHolder.iv_img3);
                }
            }
        }else if(getItemViewType(position) == BIG_IMG){
            BigImgViewHolder bigImgViewHolder = (BigImgViewHolder) holder;
            setNetPicture(imgSrc, bigImgViewHolder.iv_img);
            //区分视频或是图片
            if(resourceType == 0){
                bigImgViewHolder.iv_play.setVisibility(View.GONE);
                bigImgViewHolder.tv_bottom_right.setVisibility(View.GONE);
            }else if(resourceType == 1){
                bigImgViewHolder.tv_bottom_right.setText(videoTotalDuration);
            }

        }

        if(newsListBean.getLogo() == null){
            baseViewHolder.logo.setVisibility(View.GONE);
        }
        if(newsListBean.getFromWhere() == null){
            baseViewHolder.fromWhere.setVisibility(View.GONE);
        }
        if(newsListBean.getInteractions() == null){
            baseViewHolder.interactions.setVisibility(View.GONE);
        }

        baseViewHolder.title.setText(title);
        baseViewHolder.fromWhere.setText(fromWhere);
        baseViewHolder.interactions.setText(interactions+ ResStringUtil.getString(R.string.detail_comment));
        baseViewHolder.newsTime.setText(newsTime);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // IRecyclerView的Adapter会默认多出两个头部View，需要减去2个position
                int pos = baseViewHolder.getAdapterPosition();
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(baseViewHolder.itemView, pos);
                }
            }
        });

    }


    @Override
    public int getItemViewType(int position) {
        int viewType = ONLY_TEXT;
        NewsListBean newsListBean = mNewsListBeanList.get(position);
        ArrayList<NewsListBean.ImgBean> imgList = (ArrayList<NewsListBean.ImgBean>) newsListBean.getImgBeanList();
//        int cellType = newsListBean.getCellType();
//        int cellType = 4;
        if(imgList.size()>0 && imgList.size() < 3){
            viewType = SMALL_IMG;
        }else if(imgList.size() >= 3){
            viewType = THREE_IMG;
        }
//        else if(cellType == 3){
//            viewType = BIG_IMG;
//        }
        return viewType;
    }

    @Override
    public int getItemCount() {
        return mNewsListBeanList.size();
    }

    private void setNetPicture(String url, ImageView img) {
        Glide
                .with(mContext)
                .load(url)
                .transition(new DrawableTransitionOptions().crossFade())
                .apply(new RequestOptions().placeholder(R.mipmap.ic_default)
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(img);
    }


    class BaseViewHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView logo;
        private TextView fromWhere;
        private TextView interactions;
        private TextView newsTime;

        private BaseViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.tv_title);
            logo = (TextView) itemView.findViewById(R.id.tv_tag);
            fromWhere = (TextView) itemView.findViewById(R.id.tv_author);
            interactions = (TextView) itemView.findViewById(R.id.tv_comment_num);
            newsTime = (TextView) itemView.findViewById(R.id.tv_time);
        }
    }

    class OnlyTextViewHolder extends BaseViewHolder{
        //纯文本布局
        private OnlyTextViewHolder(View itemView){
            super(itemView);
        }
    }

    class SmallImgViewHolder extends BaseViewHolder{
        //右边一张小图的布局
        private ImageView iv_img;
        private ImageView iv_play;
        private TextView tv_duration;
        private LinearLayout ll_duration;
        private SmallImgViewHolder(View itemView){
            super(itemView);
            iv_img = (ImageView) itemView.findViewById(R.id.iv_img);
            iv_play = (ImageView) itemView.findViewById(R.id.iv_play);
            tv_duration = (TextView)itemView.findViewById(R.id.tv_duration);

            ll_duration = (LinearLayout)itemView.findViewById(R.id.ll_duration);
        }
    }

    class ThreeImgViewHolder extends BaseViewHolder{
        //下面三张图的布局
        private ImageView iv_img1;
        private ImageView iv_img2;
        private ImageView iv_img3;

        private ThreeImgViewHolder(View itemView){
            super(itemView);
            iv_img1 = (ImageView)itemView.findViewById(R.id.iv_img1);
            iv_img2 = (ImageView)itemView.findViewById(R.id.iv_img2);
            iv_img3 = (ImageView)itemView.findViewById(R.id.iv_img3);
        }
    }

    class BigImgViewHolder extends BaseViewHolder{
        //底部一张大图的布局
        private ImageView iv_img;
        private ImageView iv_play;
        private TextView tv_bottom_right;

        private BigImgViewHolder(View itemView){
            super(itemView);
            iv_img = (ImageView)itemView.findViewById(R.id.iv_img);
            iv_play = (ImageView)itemView.findViewById(R.id.iv_play);
            tv_bottom_right = (TextView)itemView.findViewById(R.id.tv_bottom_right);
        }
    }

    /**
     * 设置Item点击监听
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface OnItemClickListener<T> {
        void onItemClick(View v, int position);
    }
}
